﻿// $Id: STSettingsEditor.cs 64 2008-05-06 18:29:47Z nvivo $

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DBLGen.Configuration;

namespace DBLGen.Plugins.Export
{
    public partial class STSettingsEditor : DBLGen.UI.PluginSettingsEditor
    {
        public STSettingsEditor()
        {
            InitializeComponent();
        }

        protected new STSettings Settings
        {
            get
            {
                return (STSettings) base.Settings;
            }
            set
            {
                base.Settings = value;
            }
        }

        private void linkAddTemplate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DBLGenSection section = DBLGenSection.Load();

            string basePath = PathUtility.GetAbsolutePath(section.Export.BaseTemplateDir);
            
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "StringTemplate Group Files|*.stg|All Files|*.*";
            
            if (Directory.Exists(basePath))
                ofd.InitialDirectory = basePath;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string relativeFileName = PathUtility.GetRelativePath(ofd.FileName);
                
                bool exists = false;

                foreach (string templatePath in Settings.Templates)
                    if (String.Compare(relativeFileName, templatePath, true) == 0)
                        exists = true;

                if (!exists)
                    Settings.Templates.Add(relativeFileName);

                RefreshList();
            }
        }

        private void RefreshList()
        {
            lbTemplates.Items.Clear();

            foreach (string template in Settings.Templates)
                lbTemplates.Items.Add(template);
        }

        private void STSettingsEditor_LoadingSettings(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void STSettingsEditor_SavingSettings(object sender, EventArgs e)
        {
            Settings.Templates.Clear();

            foreach (string item in lbTemplates.Items)
                Settings.Templates.Add(item);
        }

        private void linkRemoveTemplate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (lbTemplates.SelectedIndex != -1)
                lbTemplates.Items.RemoveAt(lbTemplates.SelectedIndex);
        }
    }
}
